package cn.stylefeng.guns.modular.school.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.ClassInfo;
import cn.stylefeng.guns.modular.school.entity.ExamInfo;
import cn.stylefeng.guns.modular.school.mapper.ExamInfoMapper;
import cn.stylefeng.guns.modular.school.model.params.ExamInfoParam;
import cn.stylefeng.guns.modular.school.model.params.ExamScoreParam;
import cn.stylefeng.guns.modular.school.model.params.StudentInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ExamInfoResult;
import cn.stylefeng.guns.modular.school.model.result.StudentInfoResult;
import cn.stylefeng.guns.modular.school.service.ClassInfoService;
import cn.stylefeng.guns.modular.school.service.ExamInfoService;
import cn.stylefeng.guns.modular.school.service.ExamScoreService;
import cn.stylefeng.guns.modular.school.service.StudentInfoService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 考试信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-11-29
 */
@Service
public class ExamInfoServiceImpl extends ServiceImpl<ExamInfoMapper, ExamInfo> implements ExamInfoService {

    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private ExamScoreService examScoreService;

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void add(ExamInfoParam param) {
        for (Integer classId : param.getClassIds()) {
            ExamInfo entity = getEntity(param);
            entity.setDelFlag(false);
            entity.setCreateTime(new Date());
            entity.setCreateUser(ShiroKit.getUser().getId());
            entity.setUpdateTime(new Date());
            entity.setUpdateUser(ShiroKit.getUser().getId());
            //ClassInfo classInfo = classInfoService.findByTeacherId(ShiroKit.getUser().getId());
            entity.setClassId(classId);
            this.save(entity);

            // 参加考试的本班学生
            StudentInfoParam studentInfoParam = new StudentInfoParam();
            studentInfoParam.setClassId(classId);
            List<StudentInfoResult> studentInfoResultList = studentInfoService.findListBySpec(studentInfoParam);
            studentInfoResultList.forEach(studentInfoResult -> {
                ExamScoreParam examScoreParam = new ExamScoreParam();
                examScoreParam.setExamId(entity.getId());
                examScoreParam.setStudentId(studentInfoResult.getId());
                examScoreParam.setDelFlag(false);
                examScoreParam.setCreateTime(new Date());
                examScoreParam.setCreateUser(ShiroKit.getUser().getId());
                examScoreParam.setUpdateTime(new Date());
                examScoreParam.setUpdateUser(ShiroKit.getUser().getId());
                examScoreService.add(examScoreParam);
            });
        }
    }

    @Override
    public void delete(ExamInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ExamInfoParam param) {
        ExamInfo oldEntity = getOldEntity(param);
        ExamInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setUpdateTime(new Date());
        newEntity.setUpdateUser(ShiroKit.getUser().getId());
        this.updateById(newEntity);
    }

    @Override
    public ExamInfoResult findBySpec(ExamInfoParam param) {
        return null;
    }

    @Override
    public List<ExamInfoResult> findListBySpec(ExamInfoParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ExamInfoParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ExamInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ExamInfo getOldEntity(ExamInfoParam param) {
        return this.getById(getKey(param));
    }

    private ExamInfo getEntity(ExamInfoParam param) {
        ExamInfo entity = new ExamInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
